package com.leff.cards.web.requests;

import java.util.ArrayList;

import org.json.simple.JSONArray;

import android.util.Log;

import com.leff.cards.data.Group;
import com.leff.cards.util.Secrets;
import com.leff.cards.util.Dictionary;
import com.leff.cards.web.WebRequest;
import com.leff.notifications.NotificationCenter;

public class GetGroupsRequest extends WebRequest implements Secrets {

	public static final String GROUPS_RECEIVED = "GroupsReceived";
	
	public GetGroupsRequest(long userId) {
		super(WebCommand.GET_GROUPS);

		mParameters.put(Parameter.ID, userId);
	}

	@Override
	protected void parseResultDictionary(Dictionary dict) {
		
		JSONArray groupArray = (JSONArray)dict.get("groups");
		
		ArrayList<Group> groups = new ArrayList<Group>();
		for(Object O : groupArray) {
			Dictionary group = Dictionary.fromJSONObject(O);
			groups.add(Group.fromDictionary(group));
		}
		
		Log.v(TAG, "Completed  parsing GET_GROUPS response");
		
		NotificationCenter.postNotification(GROUPS_RECEIVED, groups);
	}
}
